package com.alpinfra.subtunoid;

import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends Activity 
{
	// Log
	private static final String TAG = "Subtunoid";

	// Communication bluetooth
	BTComm btc;

	// TextView
	TextView tvEGT;
	TextView tvAFR;
	TextView tvBoost;	  	 

	// Graph	 	 
	Graph EGTGraph;
	Graph AFRGraph;
	Graph BoostGraph;
	
	

	// Update GUI
	final Handler myHandler = new Handler(); 
	final Runnable myRunnable = new Runnable() 
	{
		public void run() 
		{						
			tvEGT.setText(String.valueOf(btc.EGTv));		        		       
			tvAFR.setText(String.valueOf(btc.AFRv));
			tvBoost.setText(new DecimalFormat("#.##").format(btc.Boostv));				
			EGTGraph.addData(btc.EGTv);		       
			AFRGraph.addData(btc.AFRv);
			BoostGraph.addData(btc.Boostv);		       
		}
	};


	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tvEGT = (TextView)findViewById(R.id.EGT);
		tvAFR = (TextView)findViewById(R.id.AFR);
		tvBoost = (TextView)findViewById(R.id.Boost);        

		// Communication
		btc = new BTComm(this,myHandler,myRunnable);

		// Graph	    	   
		EGTGraph = new Graph(this, "EGT", 200, 900, 700, 750);	   
		AFRGraph = new Graph(this, "AFR", 9, 16, 0, 0);	   	   
		BoostGraph = new Graph(this, "Boost", 0, 1.4, 1.25, 1.3);

		LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);	    
		layout.addView(EGTGraph._graphView);	  

		layout = (LinearLayout) findViewById(R.id.graph2);	    
		layout.addView(AFRGraph._graphView);

		layout = (LinearLayout) findViewById(R.id.graph3);	    
		layout.addView(BoostGraph._graphView);	  

		Log.d(TAG, "...onCreate");
	}

	@Override
	public void onResume() 
	{
		super.onResume();	    
		btc.onResume();	   	    
	}

	@Override
	public void onPause() 
	{		
		btc.onPause();
		super.onPause();	  	    	    	  
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
